<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>
        台账信息编辑
    </h1>
</section>
<!--编辑模态框-->
<div class="modal fade" id="modal-addLedger">
    <div class="modal-dialog" style="width: 500px; height: 600px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">台账信息编辑</h4>
            </div>
            <form id="ledgerForm" class="form-horizontal" role="form" style="margin-right: 70px">
                <div class="modal-body">
                    <input type="hidden" id="imiItyId" name="imiItyId" th:value="${imiItyId}">
                    <input type="hidden" id="imiId" name="imiId">
                    <div class="form-group">
                        <label class="col-sm-4 control-label no-padding-right">字段名</label>
                        <div class="col-sm-8">
                            <input type="text" id="imiField" name="imiField"
                                   class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label no-padding-right">值</label>
                        <div class="col-sm-8">
                            <input type="text" id="imiValue" name="imiValue"
                                   class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label no-padding-right">台账单位</label>
                        <div class="col-sm-8">
                            <input type="text" id="imiUnit" name="imiUnit"
                                   class="form-control">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-info file-contract btn-file" id="btn-ledger-commit">保存</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</div>
<section class="content">
    <div class="box">
        <form id="fileForm">
            <div class="box-header">
                <div class="col-lg-10 col-md-10">
                    <div  style="float: left; display: block; margin-top: 5px;">
                        <input type="file" multiple="multiple" name="file">
                        <input type="hidden" id="ityId" name="ityId" th:value="${imiItyId}">
                    </div>
                    <button type="button" class="btn btn-success" id="btn-file" style="float: left;display: block;" onclick="uploadFile()">上传</button>
               </div>
                <div class="col-lg-2 col-md-2">
                    <div th:each="file:${hAnnex}" style="width: 100%; line-height: 30px; height: 30px;">
                        <a th:href="@{${file.anxUrl}}" target="_blank" th:text="${file.anxName}"></a>
                        <button type="button" id="btn-file-del" th:onclick="|delFile(${file.anxId})|"
                                class="btn-circle btn-xs pull-right" style="color: red"><span
                                class="glyphicon glyphicon-remove"></span></button>
                    </div>
                </div>
                <div id="toolbar">
                    <button type="button" shiro:hasPermission="halinv:halledgeredit:edit" class="btn btn-info pull-right" id="btn-addLedger">新增台账字段</button>
                </div>
            </div>
            <div class="box-body">
                <div class=" col-lg-12 col-md-12">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </form>
    </div>
</section>
<div th:include="include :: footer"></div>

<script th:inline="javascript">

    /**加载表格*/
    var table;

    /** 修改台账*/
    function updateLedger(id) {
        var rows = $('#bootstrap-table').bootstrapTable('getRowByUniqueId', id);//行的数据
        js.modal.open("modal-addLedger");
        $("#ledgerForm").fillData(rows);
    }

    /**上传台账文件*/
    function uploadFile() {
        js.submit({
            url: ctx + "half/halInvManage/InvInfoEdit/uploadFile",
            data: new FormData($("#fileForm")[0]),
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    location.reload();
                } else {
                    js.modal.warning(result.msg);
                }
                js.modal.closeLoading();
            }
        })
    }

    /**删除台账附件*/
    function delFile(id) {
        js.post({
            url: ctx + "half/halInvManage/InvInfoEdit/delFile",
            data: {
                anxId: id
            },
            success: function (result) {
                location.reload();
            }
        })
    }

    /**删除台账*/
    function deleteLedger(id) {
        js.modal.confirm("确定删除该字段？", function () {
            js.post({
                url: ctx + "half/halInvManage/InvInfoEdit/deleteLedger",
                data: {
                    imiId: id
                },
                success: function (result) {

                    location.reload();
                }
            })
        })
    }

    $(function () {

        /** 加载列表 */
        table = js.table.init({
            url: ctx + "half/halInvManage/InvInfoEdit/ledgerList/" + $("#imiItyId").val(),
            pageSize: 300,
            uniqueId: 'imiId',
            showExport: false,
            columns: [
                {
                    title: '序号', field: 'imiId', width: '40',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '物料描述', field: 'ityMatName'},
                {title: '字段名', field: 'imiField'},
                {title: '值', field: 'imiValue'},
                {title: '台账单位', field: 'imiUnit'},
                {
                    title: '操作',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP("halinv:halledgeredit:edit")) {
                            actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="updateLedger(' + row.imiId + ')"> 修改</a>');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="deleteLedger(' + row.imiId + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });

        /** 新建台账字段*/
        $("#btn-addLedger").click(function () {
            document.getElementById("ledgerForm").reset();
            js.modal.open("modal-addLedger");
        });

        /** 保存台账字段*/
        $("#btn-ledger-commit").click(function () {

            js.submit({
                url: ctx + "half/halInvManage/InvInfoEdit/ledgerCommit",
                data: new FormData($("#ledgerForm")[0]),
                success: function (result) {
                    js.modal.hide("modal-addLedger");
                    location.reload();
                }
            })
        })

    })

</script>

</body>
</html>